<template>
	<view class="container" style="margin-bottom: 50rpx;">
		<view class="lineGray"></view>
		<view class="line-no width100">

			<view class="category-cmd">
				<view v-for="(item,index1) in category" :key="index1">
					<view @click="change(index1)" class="padding">
						<view v-if="item.child==childCategory" class="textblue14">{{item.name}}</view>
						<view class="textblack14" v-else>{{item.name}}</view>

					</view>
				</view>
			</view>
			<view class="line-v" />


			<view class="bigEmpty column-no">

				<view v-for="(item,index1) in childCategory" :key="index1">
					<view class="column-no ">
						<view class="line marginTop" style="height: 40rpx; ">
							<view class="textblack14 margin">{{item.name}}</view>
							<view class="lineGray bigEmpty"></view>
						</view>
						<view style="flex-direction: row; display: flex; flex-wrap: wrap;">


							<view v-for="(item2,index2) in item.child" :key="index2">
								<view class="textBlack14 bg-gray padding marginLeft marginTop" @click="select(item2)">
									<view v-if="item2.selected" class="textblue14">{{item2.name}}</view>
									<view class="textblack14" v-else>{{item2.name}}</view>
								</view>
							</view>

						</view>
					</view>
				</view>

			</view>



		</view>
		<view class="bottom-fix-1 width100 bgWhite column" style="height: 150rpx;">
			<view class="lineGray width100"></view>
			<view class="line width100 ">
				<view class="bigEmpty padding">已选择{{num}}个Tag</view>
				<view class="padding bg-more-color-round-rect textWhite margin" @click="click()">前往生成</view>
			</view>

		</view>

	</view>
</template>

<script>
	import request from '/common/request.js';
	export default {
		data() {
			return {
				category: [],
				childCategory: [],
				num: 0,
				text: "",

			}
		},
		onShareAppMessage(res) {
			return {
				title: '设辑-产品设计智能生成AI',
				path: '/pages/HomePage/HomePage',
				imageUrl: '/static/black-logo.png'
			}
		},
		// 自定义页面的分享到朋友圈
		onShareTimeline(res) {
			return {
				title: '设辑-产品设计智能生成AI',
				path: '/pages/HomePage/HomePage',
				imageUrl: '/static/black-logo.png'
			}
		},
		onShow: function() {

		},
		onReady: function() {
			this.requestInfo();
		},
		methods: {

			requestInfo() {
				const url = getApp().globalData.apiUrl + "/api/v1/category/all";
				const that = this;
				request(url, "GET", {}, uni.getStorageSync("token"))
					.then(function(res) {
						console.log('返回正确回调', res)
						if (res.code == 0) {
							var list = res.data;
							that.category = list;
							if (list.length > 0)
								that.childCategory = list[0].child
						}
					}, function(err) {
						console.log('返回错误回调', err)
					})
			},

			change(index) {

				console.log(index)
				this.num = 0
				this.childCategory = this.category[index].child
				for (var k = 0; k < this.childCategory.length; k++) {

					var item = this.childCategory[k];
					for (var m = 0; m < item.child.length; m++) {
						var item1 = item.child[m];
						if (item1.selected)
							this.num++;
					}
				}
			},
			select(item) {
				console.log(item)
				if (item.selected) {
					this.$set(item, "selected", false);
					this.num--;
				} else {
					this.$set(item, "selected", true);
					this.num++;
				}
			},
			click(id) {
				var text="";
				for (var k = 0; k < this.childCategory.length; k++) {
				
					var item = this.childCategory[k];
					for (var m = 0; m < item.child.length; m++) {
						var item1 = item.child[m];
						if (item1.selected)
							text=text+item1.name+" "
					}
				}
				
				uni.$emit('codeCreate', {
					data: text
				})
				uni.navigateBack()
			}

		}
	}
</script>

<style>
	@import url("../../common/uni.css");

	.container {

		min-height: 100vh;
	}

	.category-cmd {
		min-height: 100vh;
		width: 200rpx;
		min-width: 200rpx;
	}

	.line-v {
		min-height: 100vh;
		width: 1rpx;
		background-color: #D8D8D8;
	}
</style>